package route

import (
	"aiRole/internal/api/handler"
	"aiRole/internal/api/middleware"
	"aiRole/internal/api/service"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func InitRoute() *gin.Engine {
	r := gin.Default()
	// 配置跨域
	r.Use(cors.New(cors.Config{
		AllowOrigins: []string{"*"}, // 允许所有来源
		AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
	}))

	authHandler := handler.NewAuthHandler(&service.AuthService{})
	roleHandler := handler.NewRoleHandler(&service.RoleService{})
	aiHandler := handler.NewAiHandler(&service.AiService{})
	sessionHandler := handler.NewSession(&service.SessionService{})
	messageHandler := handler.NewMessageHandler(&service.MessageService{})
	authGroup := r.Group("auth")
	{
		authGroup.POST("/login", authHandler.Login)
		authGroup.POST("/code/:email", authHandler.Code)
	}
	r.POST("/user/register", authHandler.Register)
	r.POST("/user/register/v2", authHandler.RegisterV2)
	roleGroup := r.Group("role")
	{
		roleGroup.Use(middleware.TokenAuthMiddleware)
		roleGroup.GET("/list", roleHandler.List)
		roleGroup.POST("/add", roleHandler.AddRole)
		roleGroup.POST("/skill", roleHandler.Skill)
		roleGroup.GET("/skill/:sessionId", roleHandler.SkillList)
		roleGroup.DELETE("/:id", roleHandler.DeleteRole)
		roleGroup.GET("/:id", roleHandler.ById)
		roleGroup.GET("/search", roleHandler.Search)
	}
	aiGroup := r.Group("ai")
	{
		aiGroup.Use(middleware.TokenAuthMiddleware)
		aiGroup.POST("/asr", aiHandler.Asr)
		aiGroup.POST("/v1/chat", aiHandler.Chat)
		aiGroup.GET("/v2/chat", aiHandler.ChatV2)
		aiGroup.POST("/TTS", aiHandler.TTS)
	}
	sessionGroup := r.Group("session")
	{
		sessionGroup.Use(middleware.TokenAuthMiddleware)
		sessionGroup.POST("/:roleId", sessionHandler.AddSession)
		sessionGroup.GET("/list", sessionHandler.List)
		sessionGroup.GET("/:id", sessionHandler.ById)
	}
	messageGroup := r.Group("message")
	{
		messageGroup.Use(middleware.TokenAuthMiddleware)
		messageGroup.GET("/page/:sessionId/:page/:pageSize", messageHandler.Page)
		messageGroup.GET("/list/:sessionId", messageHandler.List)
	}
	return r
}
